// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "CaeRTSAClientSocket.h"
#include "../cae_common/CaeLogConfig.h"
#include "../cae_common/CaeMsgCode.h"


CaeRTSAEventHandler::CaeRTSAEventHandler(IStreamEngineEventHandler *streamEngineEventHandler)
{
    m_streamEngineEventHandler = streamEngineEventHandler;
}

CaeRTSAEventHandler::~CaeRTSAEventHandler()
{
}

void CaeRTSAEventHandler::onError(int code, const char *msg)
{
    LOGE("RTSA OnError code(%d): %s", code, msg);
}

void CaeRTSAEventHandler::onWarning(int code, const char *msg)
{
    LOGW("RTSA OnWarning code(%d): %s", code, msg);
}

void CaeRTSAEventHandler::onJoinRoomSuccess(const char *userId, const char *roomId, int elapsed)
{
    if (m_streamEngineEventHandler) {
        m_streamEngineEventHandler->OnJoinRoomSuccess(userId, roomId, elapsed);
    }
}

void CaeRTSAEventHandler::onRemoteUserOnline(const char *userId)
{
    if (m_streamEngineEventHandler) {
        m_streamEngineEventHandler->OnRemoteUserOnline(userId);
    }
}

void CaeRTSAEventHandler::onRemoteUserOffline(const char *userId, int reason)
{
    if (m_streamEngineEventHandler) {
        m_streamEngineEventHandler->OnRemoteUserOffline(userId, reason);
    }
}

void CaeRTSAEventHandler::onRequestKeyFrame(const char *userId,
                                            const huawei::rtsa::HRTSAStreamType streamType)
{
    if (m_streamEngineEventHandler) {
        m_streamEngineEventHandler->OnRequestKeyFrame(userId, streamType);
    }
}

void CaeRTSAEventHandler::onRemoteCommandPacket(const char *userId, const unsigned char *packet, unsigned int length)
{
    if (m_streamEngineEventHandler) {
        m_streamEngineEventHandler->OnRemoteCommandPacket(userId, packet, length);
    }
}

void CaeRTSAEventHandler::onLocalVideoBitrateUpdate(const huawei::rtsa::HRTSAStreamType streamType, unsigned int targetEncBitrate)
{
    if (m_streamEngineEventHandler) {
        m_streamEngineEventHandler->OnLocalVideoBitrateUpdate(streamType, targetEncBitrate);
    }
}

void CaeRTSAEventHandler::onLocalVideoStatsNotify(const huawei::rtsa::UpNetworkVideoStatics *connectionStatics,
        const huawei::rtsa::VideoSendStaticsInfo *videoSendStatics)
{
    if (m_streamEngineEventHandler) {
        m_streamEngineEventHandler->OnLocalVideoStatsNotify(connectionStatics, videoSendStatics);
    }
}

void CaeRTSAEventHandler::onLocalAudioStatsNotify(const huawei::rtsa::AudioSendStaticsInfo *audioSendStatics)
{
    if (m_streamEngineEventHandler) {
        m_streamEngineEventHandler->OnLocalAudioStatsNotify(audioSendStatics);
    }
}

void CaeRTSAEventHandler::onTransportReady()
{
    if (m_streamEngineEventHandler) {
        m_streamEngineEventHandler->OnTransportReady();
    }
}

void CaeRTSAEventHandler::onTransportClosed()
{
    if (m_streamEngineEventHandler) {
        m_streamEngineEventHandler->OnTransportClosed();
    }
}
